import { GoodsInfo, IApiRes } from '@/types/data'
import request from '@/utils/request'
import { defineStore } from 'pinia'
export default defineStore('goods', {
  state: () => {
    return {
      info:{} as GoodsInfo
    }
  },
  actions:{
    // 用户在页面上切换选中的规格 更新库存价钱
    updatePrice(id:string){
      const g = this.info.skus.find(it=>it.id===id)
      if(g){
        this.info.price=g.price
        this.info.oldPrice=g.oldPrice
        this.info.inventory=g.inventory
      }
    },
    async getGoodsInfo(id:string){
      const res = await request.get<IApiRes<GoodsInfo>>('/goods',{params:{id:id}})
      console.log('详情',res.data.result)
      this.info=res.data.result
    }
  }
})